<?php

namespace App\Http\Controllers;

use App\Models\Area;
use App\Models\Car;
use App\Models\Complaint;
use App\Models\Cts;
use App\Models\Flow;
use App\Models\Guide;
use App\Models\Hotel;
use App\Models\Notice;
use App\Models\Rural;
use App\Models\Scene;
use Carbon\Carbon;

class IndexController extends AuthController
{

    public function index(){
        return response()->json();
    }

    public function scene(){
        $list = Scene::all('id','title','tel','latlng','level');
        foreach ($list as $val){
            $val->monitors;
            $val->level=config('options.scene_level')[$val->level];
        }
        return response()->json($list);
    }
    public function area(){

        foreach (Area::all() as $k=>$val){
            $data[$k]['name']=$val->title;
            $data[$k]['value']=explode(',',$val->latlng);
            $count = $val->scenes()->sum('current_flow');
            array_push($data[$k]['value'],$count);
        }

        return response()->json([
            'flow'=>[
                'today'=>Flow::where('date',Carbon::today()->toDateString())->where('time',0)->sum('num'),
                'yesterday'=>Flow::where('date',Carbon::yesterday()->toDateString())->where('time',0)->sum('num'),
            ],
            'area'=>$data
        ]);
    }
    public function integrity(){
        return response()->json([
            ['title'=>'荣誉榜','val'=>Cts::where('level','<=',2)->count()],
            ['title'=>'曝光榜','val'=>Cts::where('level','>',2)->count()],
            ['title'=>'旅游通知','val'=>Notice::count()],
            ['title'=>'旅游投诉','val'=>Complaint::where('status',0)->count()],
        ]);
    }
    public function operation(){

        return response()->json([
            ['name'=>'旅游车辆','value'=>Car::count()],
            ['name'=>'导游','value'=>Guide::count()],
            ['name'=>'旅行社','value'=>Cts::count()],
            ['name'=>'酒店','value'=>Hotel::count()],
            ['name'=>'乡村旅游','value'=>Rural::count()]
        ]);
    }
    public function flows(){
        $scene = Scene::where('is_docking',1)->orderByRaw('current_flow*1 desc')->get(['id','title','current_flow']);
        foreach ($scene as $val){
            $info = $val->sceneInfo()->first();
            $val->max_flow = $info['max_flow'];
        }
        return response()->json($scene);
    }

    public function notice($type=0,$module=0){
        $notice = Notice::where([
            ['type',$type],
            ['module',$module],
            ['display',1]
        ])->orderByDesc('created_at')->get();
        return response()->json($notice);
    }
    public function sceneList(){
        return response()->json(Scene::where('is_docking',1)->pluck('title','id'));
    }

    public function getNotice(){

        return response()->json();
    }

}
